//
//  LinePreTab1Cell.m
//  ImGuider
//
//  Created by llt on 2017/9/18.
//  Copyright © 2017年 AudioCtrip. All rights reserved.
//

#import "LinePreTab1Cell.h"

#import "LinePreTab1CCell1.h"
#import "LinePreTab1CCell2.h"
#import "RewardView.h"
#import "ChargeMoneyVC.h"
#import "RewardRecordVC.h"

@interface LinePreTab1Cell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, RewardViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *guideView;
@property (weak, nonatomic) IBOutlet UIImageView *guideHeadImageView;
@property (weak, nonatomic) IBOutlet UILabel *guideDescribeLabel;
@property (weak, nonatomic) IBOutlet UILabel *guideNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *guideTagLabel;
@property (weak, nonatomic) IBOutlet UIImageView *guideVipImageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *guideNameTopConstraint;

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *mapImageViewH;// 高度
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *mapImageViewTop;
@property (weak, nonatomic) IBOutlet UILabel *recordDescribeLabel;

@property (weak, nonatomic) IBOutlet UIImageView *mapImageView;

@property (weak, nonatomic) IBOutlet UIButton *allPointBtn;

@property (weak, nonatomic) IBOutlet UIView *rewardBtnView;
@property (weak, nonatomic) IBOutlet UILabel *rewardLabel;

@property (nonatomic, strong)RewardView * rewardView;

@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation LinePreTab1Cell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self.guideView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(guideViewAction)]];
    self.recordDescribeLabel.text = MYLocalizedString(@"SITEPLAY_RECORD", nil);
    [self.allPointBtn setTitle:MYLocalizedString(@"PUBLIC_SHOW_ALL", nil) forState:UIControlStateNormal];
    
    [self.allPointBtn setTitleColor:kThemeColor forState:UIControlStateNormal];
    self.rewardLabel.text = MYLocalizedString(@"COIN_REWARD", nil);
    
    @weakify(self);
    [self.rewardBtnView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        
        @strongify(self);
        
        if (![IGUtil hasLoginOrPresentLogin]) {
            
            return;
        }
        
        self.superVC.sl_popupController.layoutType = PopupLayoutTypeBottom;
        [self.superVC.sl_popupController presentContentView:self.rewardView];
        
        self.rewardView.lineid = self.linemodel.lineid;
    }]];
}

- (RewardView *)rewardView {
    
    if (!_rewardView) {
        
        _rewardView = [[RewardView alloc] initWithFrame:CGRectMake(0, (kScreenHeight - RewardH), kScreenWidth, RewardH)];
        _rewardView.delegate = self;
    }
    
    return _rewardView;
}

- (void)setLinemodel:(LineModel *)linemodel {
    
    _linemodel = linemodel;
    
    [self.guideHeadImageView setImageURL:[NSURL URLWithString:linemodel.guide.headimg] placeholderImage:[UIImage imageNamed:@"default"]];
    
    self.guideDescribeLabel.text = linemodel.guide.introduction;
    self.guideNameLabel.text = linemodel.guide.realname;
    self.guideTagLabel.text = linemodel.guide.certificate;
    
    self.guideVipImageView.hidden = !linemodel.guide.vip;
    
    if (linemodel.guide.certificate.length == 0) {
        self.guideNameTopConstraint.constant = 20;
    }
    if (linemodel.linepics.count > 0) {
        
        [self.mapImageView setImageURL:[NSURL URLWithString:linemodel.linepics.firstObject] placeholderImage:[UIImage imageNamed:@"placeholder_rec"]];
        self.mapImageViewH.constant = (kScreenWidth - 32) * 9 / 16;
    } else {
        
        self.mapImageView.hidden = YES;
        
        self.mapImageViewH.constant = 0;
        self.mapImageViewTop.constant = 0;
    }
    
    self.dataSource = [NSMutableArray arrayWithArray:linemodel.records];
    
    if (self.dataSource.count > 0) {
        [self.dataSource removeObjectAtIndex:0];
    }
    
    [self.collectionView reloadData];
}

- (void)guideViewAction {
    
    [_delegate jumpguider];
}

- (IBAction)goAllRecordPointVC:(UIButton *)sender {
    
    if (_delegate && [_delegate respondsToSelector:@selector(jumpAllPoint)]) {
        [_delegate jumpAllPoint];
    }
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
//    if (self.linemodel.type.integerValue == 1) {
//        return self.dataSource.count * 2 - 1;
//    }
//    return self.dataSource.count;
    return self.dataSource.count * 2 - 1;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ScenicPoint *record = self.dataSource[indexPath.row / 2];
    
//    if (self.linemodel.type.integerValue == 1) {
//
//        record = self.dataSource[indexPath.row / 2];
//    } else {
//        record = self.dataSource[indexPath.row];
//    }
    
    
    if (indexPath.row % 2 != 0) {

        LinePreTab1CCell2 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell2" forIndexPath:indexPath];
        cell.transport = record.transport.length == 0 ? self.linemodel.transport.integerValue : record.transport.integerValue;

        return cell;
    }

    LinePreTab1CCell1 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
    cell.record = record;

    return cell;
    
}

#pragma mark - UICollectionViewDelegateFlowLayout

//每个单元格的大小size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
     if (indexPath.row % 2 != 0) {
         
         return CGSizeMake(24, 102);
     }
    
    return CGSizeMake(70, 102);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
//    if (self.linemodel.type.integerValue == 1) {
//        return 0;
//    }
    
    return 8;
}


#pragma mark  -- RewardViewDelegate

- (void)recharge {
    
    ChargeMoneyVC * moneyVC = [IGUtil loadViewController:@"ChargeMoneyVC" storyboard:@"Account"];
    
    [self.superVC.sl_popupController dismiss];
    [self.superVC.navigationController pushViewController:moneyVC animated:YES];
}

- (void)rewardWithModel:(RewardModel *)model {
    
    [NetAPIManager rewardWithLineid:self.linemodel.lineid coin:model.amount block:^(NSString *result, NSError *error) {
        
        if (!error) {
            [[NSNotificationCenter defaultCenter] postNotificationName:kReloadUserDataNotif object:nil];
            [IGUtil showToast:MYLocalizedString(@"REWARD_SUCCESS", nil)];
            [self.superVC.sl_popupController dismiss];
        }
    }];
}

- (void)showRewardRecord {
    
    RewardRecordVC *recordVC = [RewardRecordVC new];
    recordVC.unionid = self.linemodel.unionid;
    [self.superVC.sl_popupController dismiss];
    
    [self.superVC.navigationController pushViewController:recordVC animated:YES];
}


@end
